package conf

import (
	logging "github.com/sirupsen/logrus"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
	"strings"
	"webdemo/src/gin/dbconfig"
	"webdemo/src/gin/redisconfig"
)

type MysqlConf struct {
	AppMode    string `yaml:"AppMode"`
	HttpPort   string `yaml:"HttpPort"`
	Db         string `yaml:"Db"`
	DbHost     string `yaml:"DbHost"`
	DbPort     string `yaml:"DbPort"`
	DbUser     string `yaml:"DbUser"`
	DbPassWord string `yaml:"DbPassWord"`
	DbName     string `yaml:"DbName"`
}

func InitDB() {
	workDir, err := os.Getwd()
	if err != nil {
		logging.Error("os.Getwd error is ", err)
	}
	configFile, err := ioutil.ReadFile(workDir + "/src/gin/conf/config.yaml")
	conf := MysqlConf{}
	err = yaml.Unmarshal(configFile, &conf)

	if err != nil {
		logging.Error("配置文件读取错误:", err)
	}

	path := strings.Join([]string{conf.DbUser, ":", conf.DbPassWord, "@tcp(", conf.DbHost, ":", conf.DbPort, ")/", conf.DbName, "?charset=utf8mb4&parseTime=True&loc=Local"}, "")
	dbconfig.Database(path)
}

func InitRedis() {
	redisconfig.Redis()
}
